import time, logging, json, yaml

from log_parser import LogParser

logger = logging.getLogger(__name__)
FORMAT = '%(asctime)s - %(levelno)s: %(message)s'
logging.basicConfig(filename='parse_to.log', level=logging.INFO, format=FORMAT)

def get_all_info(filename) -> dict:
    if not filename:
        return None
    result = {
        'file': filename if isinstance(filename, str) else filename.name,
        'archives_list': [],
        'archives_info': [],
        'info_file': None,
        'passwords_file': None,
        'is_zip_file': False,
    }
    start_time = time.time()
    if isinstance(filename, str) and filename.split('.')[-1] != 'zip':
        logger.warning(f'{result["file"]} - File is not a zip file')
        return result
    with LogParser(filename) as ziparch:
        result['archives_list'] = ziparch.zip_files_list
        result['archives_info'] = []
        result['is_zip_file'] = True
        for arch in result['archives_list']:
            try:
                result['archives_info'].append(get_all_info(ziparch.open(arch)))
            except Exception as e:
                logger.warning(f'{result["file"]} - {e}')
                print(f'{result["file"]} - {e}')
                pass
        result['info_file'] = ziparch.infoToDf
        result['passwords_file'] = ziparch.psswrdToDf
    logger.info(f'Finished file \'{result["file"]}\' - {(time.time()-start_time):.3f} sec')
    return result

def main():
    with open('secrets/secrets_file_lst.yml', 'r') as file_list:
        f_names = yaml.safe_load(file_list)
    for f in f_names[11:]:
        start_time = time.time()
        print(f'Work with file \'{f}\'')
        z = get_all_info(f)
        print(f'Taken {(time.time() - start_time):.3f} seconds.')
        with open(f + '.json', 'w') as o_file:
            json.dump(z, o_file)

if __name__ == '__main__':
    main()
